using System;
using JetBrains.Annotations;

namespace Kaleida.ServiceMonitor.Model.StructuredSource
{
    public class OperationDefinition
    {
        private readonly ActionDefinition requestAction;
        private readonly ActionDefinition handlerAction;
        [CanBeNull] private readonly ImpersonationDefinition impersonation;

        public OperationDefinition([NotNull] ActionDefinition requestAction, [NotNull] ActionDefinition handlerAction, [CanBeNull] ImpersonationDefinition impersonation = null)
        {
            if (requestAction == null) throw new ArgumentNullException("requestAction");
            if (handlerAction == null) throw new ArgumentNullException("handlerAction");

            this.requestAction = requestAction;
            this.handlerAction = handlerAction;
            this.impersonation = impersonation;
        }

        [NotNull]
        public ActionDefinition RequestAction
        {
            get { return requestAction; }
        }

        [NotNull]
        public ActionDefinition HandlerAction
        {
            get { return handlerAction; }
        }

        [CanBeNull]
        public ImpersonationDefinition Impersonation
        {
            get { return impersonation; }
        }

        public override string ToString()
        {
            return string.Format("Request: {0}\r\nResponse Handler: {1} (user: {2})\r\n", requestAction, handlerAction, impersonation == null ? "<Logged-in user>" : impersonation.ToString());
        }
    }
}